package com.sne.airways.system.controller;

import com.sne.airways.basecore.controller.BaseController;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.system.constants.Constant;
import com.sne.airways.system.dto.TdRoleDTO;
import com.sne.airways.system.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-23
 * Time: 下午5:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    @RequestMapping("/queryRoleList")
    @ResponseBody
    public void queryRoleList(String roleNo,String roleName,String status,int start,int limit){
        List<TdRoleDTO> roleDTOList = roleService.queryRoleList(roleNo,roleName,status,start,limit);
        int count = roleService.queryRoleCount(roleNo,roleName,status);
        renderPageJsonArray(roleDTOList,count);
    }
    @RequestMapping("/saveRole")
     @ResponseBody
     public void saveRole(String opType,String roleId,String roleNo,String roleName,String roleDesc,String status,int version){
        //首先校验参数合法性
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(roleNo)||StringUtils.isBlank(roleName)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.ADD,opType)){
            roleService.saveRole(roleNo, roleName, roleDesc,status,version);
        }else if(StringUtils.equals(Constant.EDIT,opType)){
            if(StringUtils.isBlank(roleId)){
                throw new AppException("角色主键不能为空");
            }
            roleService.updateRole(roleId,roleNo, roleName, roleDesc,status, version);
        }
        renderSuccess();
    }
    @RequestMapping("/deleteRole")
    @ResponseBody
    public void deleteRole(String opType,String roleId,int version){
        //首先校验参数合法性
        if(StringUtils.isBlank(opType)||StringUtils.isBlank(roleId)){
            throw new AppException("获取参数出错");
        }
        if(StringUtils.equals(Constant.DELETE,opType)){
            if(StringUtils.isBlank(roleId)){
                throw new AppException("角色主键不能为空!");
            }
            roleService.deleteRole(roleId,version);
        }
        renderSuccess();
    }
}
